package com.lingkang.sales.controller;

import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.lingkang.sales.common.val.HttpCode;
import com.lingkang.sales.config.aop.DefaultPageValid;
import com.lingkang.sales.data.dto.ResponsePageResult;
import com.lingkang.sales.data.dto.ResponseResult;
import com.lingkang.sales.data.entity.SaPermission;
import com.lingkang.sales.service.AdminService;
import com.lingkang.sales.service.AuthService;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author linke
 * @date 2020/11/14 22:18
 * @description
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

  @Autowired
  private HttpServletRequest request;

  @Autowired
  private AdminService adminService;

  @Autowired
  private AuthService authService;


  @GetMapping(value = {"index", ""})
  public String index() {
    return "admin/index";
  }

  @GetMapping("welcome")
  public String welcome() {
    request.setAttribute("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    return "admin/welcome";
  }

  @GetMapping("role")
  public String role() {
    return "admin/role";
  }

  /**
   * 添加角色
   */
  @GetMapping("roleAdd")
  public String roleAdd() {
    return "admin/role_add";
  }

  @PreAuthorize("hasPermission('/admin/roleAdd','ROLE_ADMIN:w')")//需要写权限
  @PostMapping("roleAdd")
  @ResponseBody
  public ResponseResult roleAddPost(String roleName, String desc) {
    if (StringUtils.isEmpty(roleName)) {
      return new ResponseResult(HttpCode.VALIDATION, "角色名不能为空");
    }
    if (!roleName.contains("ROLE_")) {
      return new ResponseResult(HttpCode.VALIDATION, "角色名必须以“ROLE_”开头");
    }
    return authService.addRole(roleName, desc);
  }

  @GetMapping("roleList")
  @ResponseBody
  @DefaultPageValid
  public ResponsePageResult roleList(Integer page, Integer size, String search) {
    return authService.getRoleList(page, size, search);
  }

  @PostMapping("roleDelete")
  @PreAuthorize("hasPermission('/admin/roleAdd','ROLE_ADMIN:w')")//需要写权限
  @ResponseBody
  public ResponseResult roleDelete(Integer id) {
    if (id == null) {
      return new ResponseResult(HttpCode.VALIDATION, "缺少传参：id");
    }
    return authService.roleDelete(id);
  }

  @PostMapping("roleUpdate")
  @PreAuthorize("hasPermission('/admin/roleUpdate','ROLE_ADMIN:w')")//需要写权限
  @ResponseBody
  public ResponseResult roleUpdate(Integer id, String roleName, String description) {
    if (id == null) {
      return new ResponseResult(HttpCode.VALIDATION, "id不能为空");
    }
    if (StringUtils.isEmpty(roleName)) {
      return new ResponseResult(HttpCode.VALIDATION, "角色名不能为空");
    }
    if (!roleName.contains("ROLE_")) {
      return new ResponseResult(HttpCode.VALIDATION, "角色名必须以“ROLE_”开头");
    }
    return authService.roleUpdate(id, roleName, description);
  }

  @GetMapping("permission")
  public String permission() {
    return "admin/permission";
  }

  @GetMapping("permissionList")
  @ResponseBody
  @DefaultPageValid
  public ResponsePageResult permissionList(Integer page, Integer size, String search) {
    return authService.permissionList(page, size, search);
  }

  @GetMapping("permissionEdit")
  public String permissionEdit(String roleName) {
    SaPermission permission = authService.getPermission(roleName);
    request.setAttribute("permission", permission);
    request.setAttribute("actionR", permission.getAction().indexOf("r") != -1);
    request.setAttribute("actionW", permission.getAction().indexOf("w") != -1);
    return "admin/permission_edit";
  }

  @PostMapping("permissionEdit")
  @PreAuthorize("hasPermission('/admin/roleAdd','ROLE_ADMIN:w')")//需要写权限
  @ResponseBody
  public ResponseResult permissionEditPost(String roleName, String source, String action, String description) {
    if (StringUtils.isEmpty(roleName)) {
      return new ResponseResult(HttpCode.VALIDATION, "roleName不能为空");
    }
    if (StringUtils.isEmpty(source)) {
      return new ResponseResult(HttpCode.VALIDATION, "source不能为空");
    }
    return authService.permissionUpdate(roleName, source, action, description);
  }

  @PostMapping("permissionDelete")
  @ResponseBody
  public ResponseResult permissionDelete(String roleName) {
    if (StringUtils.isEmpty(roleName)) {
      return new ResponseResult(HttpCode.VALIDATION, "roleName不能为空");
    }
    return authService.permissionDelete(roleName);
  }

  @GetMapping("permissionAdd")
  public String permissionAdd() {
    request.setAttribute("roleList", authService.getRoleListExcludeExist());
    return "admin/permission_add";
  }

  @PostMapping("permissionAdd")
  @PreAuthorize("hasPermission('/admin/permissionAdd','ROLE_ADMIN:w')")//需要写权限
  @ResponseBody
  public ResponseResult permissionAddPost(String roleName, String source, String action, String description) {
    if (StringUtils.isEmpty(roleName)) {
      return new ResponseResult(HttpCode.VALIDATION, "角色名不能为空");
    }
    if (StringUtils.isEmpty(source)) {
      return new ResponseResult(HttpCode.VALIDATION, "source不能为空");
    }
    return authService.permissionAdd(roleName, source, action, description);
  }

  @GetMapping("authorization")
  public String authorization(){
    return "/admin/authorization";
  }



  /**
   * PoolingCount 连接池
   * PoolingPeak 连接池峰值
   * PoolingPeakTime 连接池峰值时间
   * QueryTimeout 查询超时数
   * TransactionQueryTimeout 事务查询超时数
   * LoginTimeout 登录超时数
   * PhysicalConnectCount 物理连接数
   * PhysicalCloseCount 物理连接关闭数
   * ActivePeakTime 活动峰值时间
   */
  @GetMapping("/stat")
  @ResponseBody
  public Object druidStat() {
    // DruidStatManagerFacade#getDataSourceStatDataList 该方法可以获取所有数据源的监控数据，除此之外 DruidStatManagerFacade 还提供了一些其他方法，你可以按需选择使用。
    return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
  }


}
